package client_controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
import client_gui.CustomerServiceGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;
import entity.Customer;
import entity.Employee;

public class CustomerServiceController implements dbHandler {
	/**
	 * @class CustomerServiceController allows communications between customer service employees and DB to edit customer information
	 * 
	 * @param CustomerServiceGUI links to GUI.
	 * @param EmployeeLoginController links to check fields for employee authorization. 
	 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
	 * @param Customer is for a  new customer instance.
	 */
	
	private CustomerServiceGUI csg ;
	private EmployeeLoginController elc;
	private Customer customer;
	private Boss b;

	public CustomerServiceController (CustomerServiceGUI csg, EmployeeLoginController elc){
		this.setCsg(csg);
		this.setElc(elc);		
		this.getCsg().setsubmitlis(new SubmitListener());
		this.getCsg().setlogoutlis(new Logout());
		this.getCsg().setexittlis(new Exit());
		getCombo();
	}
	
	@Override
	public void Add() {
		try {
			
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void Search() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		
	}

	@Override
	public String Delete() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void HandleMsg(Object msg) {
		this.setB((Boss) msg);
		//this.setCustomer((Customer) this.getB().getObj());
		if(this.getB().getMsg().equals("ok"))
		{
			this.getCsg().getError().setText("Customer Added Successfully");
			this.getCsg().getError().setForeground(Color.BLUE);
			this.getCsg().getError().setVisible(true);
		}
		else if(this.getB().getMsg().equals("failed"))
		{
			this.getCsg().getError().setText("Customer Already Exist");
			this.getCsg().getError().setForeground(Color.red);
		}
		else if(this.getB().getMsg().equals("logout")){
			this.getCsg().dispose();
			this.getElc().getElg().setVisible(true);
		}
		else if(this.getB().getCommand().equals("getCombo")){
			//this.getCsg().dispose();
			//this.getElc().getElg().setVisible(true);
			ArrayList <String> areas ;
			areas= (ArrayList<String>) this.getB().getObj();
			for(String i :areas )
				csg.getArea().addItem(i);
		}
		
		
	}

	@Override
	public void update() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void create_boss(){
		customer=new Customer();
		initialize();
		this.setB(new Boss(customer,"add","CustomerServiceController"));

	}
	public void initialize(){
		try {
			this.getCustomer().setCid(this.csg.get_tf_id());
			this.getCustomer().setAge(this.csg.get_tf_age());
			this.getCustomer().setName(this.csg.get_tf_name());
			this.getCustomer().setPhone(this.csg.get_tf_phone());
			this.getCustomer().setArea(this.csg.getArea().getSelectedItem().toString());
		} catch (Exception e) {
			getCsg().getError().setText("Missing Fields");
        	getCsg().getError().setForeground(Color.red);
        	getCsg().getError().setVisible(true);
		}
	}

	public class SubmitListener implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        	create_boss();
        	if(getCustomer().getCid()==-1 || getCustomer().getArea().equals("") || getCustomer().getAge()==0 || getCustomer().getName().equals("") || getCustomer().getPhone().equals(""))
        		getCsg().getError().setText("Missing Fields");
        	else Add();
    	 
	 }

	}
	
	public class Logout implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		setB(new Boss(getElc().getEmp(),"logout","CustomerServiceController"));
        		update();
	
        	}
	 }
	public class Exit implements WindowListener{

		@Override
		public void windowActivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosed(WindowEvent e) {
			
			
		}

		@Override
		public void windowClosing(WindowEvent e) {
			
			setB(new Boss(getElc().getEmp(),"logout","CustomerServiceController"));
    		update();
			
		}

		@Override
		public void windowDeactivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	public CustomerServiceGUI getCsg() {
		return csg;
	}

	public void setCsg(CustomerServiceGUI csg) {
		this.csg = csg;
	}

	public EmployeeLoginController getElc() {
		return elc;
	}

	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}
	
	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public Boss getB() {
		return b;
	}

	public void setB(Boss b) {
		this.b = b;
	}
	public void getCombo(){
		customer=new Customer();
		this.setB(new Boss(customer,"getCombo","CustomerServiceController"));
		this.Search();
	}
}

